package com.ruoyi.system.service.survey.Impl;

import com.ruoyi.system.domain.survey.SurveyOption;
import com.ruoyi.system.domain.survey.SurveyQuestion;
import com.ruoyi.system.domain.survey.SurveyQuestionVo;
import com.ruoyi.system.mapper.survey.SurveyQuestionMapper;
import com.ruoyi.system.service.survey.ISurveyQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class SurveyQuestionServiceImpl implements ISurveyQuestionService {

    @Autowired
    private SurveyQuestionMapper surveyQuestionMapper;

    @Override
    public List<SurveyQuestion> selectSurveyQuestionBySurveyId(Long surveyId) {
        List<SurveyQuestion> list = surveyQuestionMapper.selectSurveyQuestionBySurveyId(surveyId);
        for (SurveyQuestion question : list) {
            System.out.println(question);  // 默认会调用 SurveyQuestion 的 toString 方法
        }
        return list;
    }

    /**
     * 通过surveyId更新问题和选项等
     *
     * @author sergio
     * @date 2024/11/27
     */
    @Override
    public int updateQuestionBySurveyId(SurveyQuestionVo surveyQuestionVo){
        // 1. 获取现有的 SurveyQuestion 数据
        List<SurveyQuestion> existingQuestions = surveyQuestionMapper.selectSurveyQuestionBySurveyId(Long.valueOf(surveyQuestionVo.getSurveyId()));
        // 2. 遍历传入的 SurveyQuestion 列表
        for (SurveyQuestion newQuestion : surveyQuestionVo.getSurveyQuestion()) {
            boolean exists = false;  // 标记当前问题是否存在

            // 3. 遍历现有问题，判断是否存在
            for (SurveyQuestion existingQuestion : existingQuestions) {
                if (existingQuestion.getQuestionId().equals(newQuestion.getQuestionId())) {
                    // 问题存在，进行更新
                    int questionId = surveyQuestionMapper.updateSurveyQuestion(newQuestion);  // 更新问题
                    System.out.println(questionId);
                    newQuestion.setQuestionId(questionId);
                    exists = true;

                    // 4. 处理问题下的选项数据
                    for (SurveyOption newOption : newQuestion.getSurveyOptionList()) {
                        boolean optionExists = false;
                        for (SurveyOption existingOption : existingQuestion.getSurveyOptionList()) {
                            if (existingOption.getOptionId().equals(newOption.getOptionId())) {
                                // 选项存在，更新
                                surveyQuestionMapper.updateSurveyOption(newOption);
                                optionExists = true;
                                break;
                            }
                        }
                        if (!optionExists) {
                            // 选项不存在，新增
                            surveyQuestionMapper.insertSurveyOption(newOption);
                        }
                    }
                    break;  // 找到对应问题，跳出内层循环
                }
            }

            // 5. 如果问题不存在，新增该问题
            if (!exists) {
                newQuestion.setSurveyId(surveyQuestionVo.getSurveyId());
                surveyQuestionMapper.insertSurveyQuestion(newQuestion);  // 新增问题
                // 6. 同时新增该问题下的选项
                for (SurveyOption newOption : newQuestion.getSurveyOptionList()) {
                    newOption.setQuestionId(newQuestion.getQuestionId());
                    surveyQuestionMapper.insertSurveyOption(newOption);  // 新增选项
                }
            }
        }

        return 0;
    }

}
